package org.zju.moviemotion;

import java.util.Random;

import org.zju.moviemotion.widget.KeywordsView;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/****
 * The Class CoverFlowTestingActivity.
 */
public class ResultActivity extends Activity implements OnClickListener {

	private TextView tv_letter;
	private TextView tv_age;
	private ImageButton forward;
	private ImageButton back;
	private RelativeLayout layout;
	private String[] totalKeys = null;
	private char[] key_words;
	private KeywordsView showKeywords = null;
	private LinearLayout searchLayout = null;
	private GestureDetector mggd;
	private boolean isStart = false;// 判断是否已经开始字母动画

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.result);

		forward = (ImageButton) findViewById(R.id.forward);
		forward.setOnClickListener(this);
		back = (ImageButton) findViewById(R.id.back);
		back.setOnClickListener(this);
		tv_letter = (TextView) findViewById(R.id.tv_letter);
		tv_letter.setClickable(true);
		tv_age = (TextView) findViewById(R.id.tv_age);
		tv_age.setText(String.valueOf(MovieAplication.getInstance().getAge()));
		layout = (RelativeLayout) findViewById(R.id.layout);
		layout.setBackgroundColor(MovieAplication.getInstance().getColor());

		DisplayMetrics dm = getResources().getDisplayMetrics();
		final int screenWidth = dm.widthPixels;
		final int screenHeight = dm.heightPixels - 50;
		// 添加触摸事件
		tv_letter.setText(MovieAplication.getInstance().getMotion());
		key_words = MovieAplication.getInstance().getMotion().toCharArray();
		tv_letter.setOnTouchListener(new OnTouchListener() {
			int lastX, lastY; // 记录移动的最后的位置

			public boolean onTouch(View v, MotionEvent event) {
				// 获取Action

				int ea = event.getAction();

				Log.i("TAG", "Touch:" + ea);
				switch (ea) {
				case MotionEvent.ACTION_DOWN: // 按下
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					break;
				/**
				 * layout(l,t,r,b) l Left position, relative to parent t Top
				 * position, relative to parent r Right position, relative to
				 * parent b Bottom position, relative to parent
				 * */
				case MotionEvent.ACTION_MOVE: // 移动
					// 移动中动态设置位置
					int dx = (int) event.getRawX() - lastX;
					int dy = (int) event.getRawY() - lastY;
					int left = v.getLeft() + dx;
					int top = v.getTop() + dy;
					int right = v.getRight() + dx;
					int bottom = v.getBottom() + dy;
					if (left < 0) {
						left = 0;
						right = left + v.getWidth();
					}
					if (right > screenWidth) {
						right = screenWidth;
						left = right - v.getWidth();
					}
					if (top < 0) {
						top = 0;
						bottom = top + v.getHeight();
					}
					if (bottom > screenHeight) {
						bottom = screenHeight;
						top = bottom - v.getHeight();
					}
					v.layout(left, top, right, bottom);
					Log.i("", "position：" + left + ", " + top + ", " + right
							+ ", " + bottom);
					// 将当前的位置再次设置
					lastX = (int) event.getRawX();
					lastY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP: // 脱离
					isStart = true;
					// 添加
					feedKeywordsFlow(showKeywords, key_words);
					// showKeywords.setDuration(2000L);
					showKeywords.go2Shwo(KeywordsView.ANIMATION_OUT);
					tv_letter.setVisibility(4);
					break;
				}
				return false;
			}
		});

		showKeywords = (KeywordsView) this.findViewById(R.id.word);
		// showKeywords.setDuration(20001);
		// showKeywords.setOnClickListener(this);
		this.mggd = new GestureDetector(new Mygdlinseter());
		showKeywords.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return mggd.onTouchEvent(event); // 注册点击事件
			}
		});
	}

	class Mygdlinseter implements OnGestureListener {
		@Override
		public boolean onDown(MotionEvent e) {
			return true;
		}

		@Override
		public void onShowPress(MotionEvent e) {
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (!isStart) {
				return true;
			}
			if (e2.getX() - e1.getX() > 50) { // 右滑
				showKeywords.rubKeywords();
				feedKeywordsFlow(showKeywords, key_words);
				showKeywords.go2Shwo(KeywordsView.ANIMATION_IN);
				return true;
			}
			if (e2.getX() - e1.getX() < -50) {// 左滑
				showKeywords.rubKeywords();
				feedKeywordsFlow(showKeywords, key_words);
				showKeywords.go2Shwo(KeywordsView.ANIMATION_OUT);
				return true;
			}
			if (e2.getY() - e1.getY() < -50) {// 上滑
				showKeywords.rubKeywords();
				feedKeywordsFlow(showKeywords, key_words);
				showKeywords.go2Shwo(KeywordsView.ANIMATION_OUT);
				return true;
			}
			if (e2.getY() - e1.getY() > 50) {// 下滑
				showKeywords.rubKeywords();
				feedKeywordsFlow(showKeywords, key_words);
				showKeywords.go2Shwo(KeywordsView.ANIMATION_IN);
				return true;
			}
			return false;
		}
	}

	private Random random = new Random();

	private static void feedKeywordsFlow1(KeywordsView keywordsFlow, char[] arr) {
		Random random = new Random();
		for (int i = 0; i < arr.length; i++) {
			int ran = random.nextInt(arr.length);
			String tmp = String.valueOf(arr[ran]);
			keywordsFlow.feedKeyword(tmp);
		}
	}

	// 重排序
	public void feedKeywordsFlow(KeywordsView keywordsFlow, char[] arr) {
		for (int index = arr.length - 1; index >= 0; index--) {
			// 从0到index处之间随机取一个值，跟index处的元素交换
			exchange(random.nextInt(index + 1), index, arr);
		}
		for (int index = 0; index < arr.length; index++) {
			keywordsFlow.feedKeyword(String.valueOf(arr[index]));
		}
	}

	// 交换位置
	private void exchange(int p1, int p2, char[] arr) {
		char temp = arr[p1];
		arr[p1] = arr[p2];
		arr[p2] = temp;
	}

	@Override
	public void onClick(View v) {
		if (v == forward) {
			findViewById(R.id.nav).setVisibility(8);
			//保存截图
			 ScreenShot.shoot(ResultActivity.this);
		} else if (v == back) {
			finish();
		}
	}

}
